package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {

    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        //获取查询参数和分页参数的封装
        Map<String, Object> filters = super.getFilters(request);
        //进行查询数据回显给前端
        model.addAttribute("filters",filters);
        //根据封装好的参数进行查询
        PageInfo<Community> page = communityService.findPage(filters);
        //把查询到的数据放入共享域
        model.addAttribute("page",page);
        //获取区域列表List数据
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);


        //转发到正确主页面
        return "community/index";
    }

    @RequestMapping("/create")
    public String create(Model model){
        //创建页面还有区域的选择,所以需要区域列表数据
        //获取区域列表List数据
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/create";
    }

    @RequestMapping("/save")
    public String save(Community community){
        //这里有个问题,是因为community里面的averagePrice 是字符串类型,如果前端不填值这里就为""空字符串,在sql那里无法转为十进制.
        //所以这里community要么是数字的字符,要么是null
        if(community.getAveragePrice().equals("")){
            community.setAveragePrice(null);
        }
        this.communityService.insert(community);
        return "common/successPage";
    }

    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        //查询到id对应的小区信息,根据id查询community
        Community community = communityService.getById(id);
        model.addAttribute("community",community);
        //查询到区域集合.获取区域列表List数据,因为修改页面需要下拉框选择
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        //弹到修改的页面
        return "community/edit";
    }

    @RequestMapping("/update")
    public String update(Community community){
        //这里有个问题,是因为community里面的averagePrice 是字符串类型,如果前端不填值这里就为""空字符串,在sql那里无法转为十进制.
        //所以这里community要么是数字的字符,要么是null  (以用COALESCE函数在sql语句处解决)
/*        if(community.getAveragePrice().equals("")){
            community.setAveragePrice(null);
        }*/
        this.communityService.update(community);
        //重定向到小区管理页面
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        communityService.delete(id);
        return "redirect:/community";
    }
}
